package com.eerl.pdev.werl.msg.handler;

import com.eerl.pdev.ProjectContainer;
import com.eerl.pdev.debug.EVarWatch;
import com.eerl.pdev.util.EToolPluginConsoleToolWindowFactory;
import com.eerl.pdev.werl.MsgHandler;
import com.ericsson.otp.erlang.*;
import com.intellij.openapi.project.Project;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

public class BindVarHandler implements MsgHandler {

    private Project project;
    public BindVarHandler(Project project) {
        this.project = project;
    }

    @Override
    public void handle(Map<Integer, Integer> state, OtpErlangTuple object) {
        // {"com.eerl.pdev.werl.msg.handler.BindVarHandler610548625",false,null,"in [accumulate_lev_role:24] try get var[B] value fail",[]}
        // {"com.eerl.pdev.werl.msg.handler.BindVarHandler610548625",true,null,[],{"accumulate_lev_role","C",26,3}}
        // {0,                                                       1,    2,   3,4}
        try {
            boolean result = Boolean.parseBoolean((object.elementAt(1).toString()));
            String errMsg = object.elementAt(3).toString();
            OtpErlangTuple datatuple = (OtpErlangTuple) object.elementAt(4);
            if (result) {
                // {"accumulate_lev_role",26,[{varName, varValue}]}
                String mod = ((OtpErlangString) datatuple.elementAt(0)).stringValue();
                String line = datatuple.elementAt(1).toString();
                OtpErlangList nameValueTupleList = (OtpErlangList) datatuple.elementAt(2);
                nameValueTupleList.forEach((OtpErlangObject nameValueObj) -> {
                    OtpErlangTuple nameValue = (OtpErlangTuple) nameValueObj;
                    String name = ((OtpErlangAtom) nameValue.elementAt(0)).atomValue();
                    OtpErlangObject oriVal = nameValue.elementAt(1);
                    String val;
                    if (oriVal instanceof OtpErlangBinary) {
                        val = new String(((OtpErlangBinary) oriVal).binaryValue(), StandardCharsets.UTF_8);
                    } else {
                        val = oriVal.toString();
                    }
                    String out = "[%s:%s]%s = %s".formatted(mod, line, name, val);
                    EToolPluginConsoleToolWindowFactory.printSimpleToConsole(project, out);
                    ProjectContainer.getInstance(project).get(EVarWatch.class).updateWatchVar(mod, name, line, val);
                });
            } else {
                EToolPluginConsoleToolWindowFactory.errSimpleToConsole(project, errMsg);
            }
        } catch (Exception e) {
            EToolPluginConsoleToolWindowFactory.errToConsole(project, "class cast err: " + object.toString());
            EToolPluginConsoleToolWindowFactory.errToConsole(project, e.toString());
            EToolPluginConsoleToolWindowFactory.errToConsole(project, Arrays.toString(e.getStackTrace()));
            EToolPluginConsoleToolWindowFactory.errToConsole(project, e.getMessage());
        }

    }
}
